﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Configuration;
using Lbb.Configuration;
using Lbb.Web.Mvc;

namespace Lbb.Configuration
{
    public class ConfigItem
    {
        public string Name { get; set; }
        public string Value { get; set; }
        public List<ConfigItem> Items { get; set; }

        public ConfigItem()
        {
            Items = new List<ConfigItem>();
        }

        public void Load(XElement element)
        {
            Name = element.Attribute("name").Value;
            Value = element.Attribute("value").Value;
            foreach (XElement e in element.Elements("add"))
            {
                ConfigItem i = new ConfigItem();
                i.Load(e);
                Items.Add(i);
            }
        }

        /// <summary>
        /// 获取子节点
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public ConfigItem Get(string name)
        {
            return Items.SingleOrDefault(i => i.Name == name);
        }

        public List<SelectListItem> ToSelectItemList(string selectedValue, IEnumerable<SelectListItem> pExtItems = null, IEnumerable<SelectListItem> nExtItems = null)
        {
            if (string.IsNullOrEmpty(selectedValue))
            {
                selectedValue = "";
            }
            string[] selectedValueList = selectedValue.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            List<SelectListItem> listItems = new List<SelectListItem>();

            if (pExtItems != null && pExtItems.Count() > 0)
            {
                foreach (var item in pExtItems)
                {
                    listItems.Add(new SelectListItem { Text = item.Text, Value = item.Value, Selected = selectedValueList.Contains(item.Value) });
                }
            }

            foreach (ConfigItem i in Items)
            {
                listItems.Add(new SelectListItem { Text = i.Name, Value = i.Value, Selected = selectedValueList.Contains(i.Value) });
            }

            if (nExtItems != null && nExtItems.Count() > 0)
            {
                foreach (var item in nExtItems)
                {
                    listItems.Add(new SelectListItem { Text = item.Text, Value = item.Value, Selected = selectedValueList.Contains(item.Value) });
                }
            }
            return listItems;
        }

        public List<SelectListItem> SelectedItemList(string selectedProperty, string selectedValue)
        {
            if (string.IsNullOrEmpty(selectedProperty))
            {
                selectedProperty = "";
            }
            string[] selectedPropertyList = selectedProperty.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            List<SelectListItem> listItems = new List<SelectListItem>();
            if (string.IsNullOrEmpty(selectedValue))
            {
                selectedValue = "";
            }
            string[] selectedValueList = selectedValue.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            foreach (ConfigItem i in Items)
            {
                if (selectedPropertyList.Contains(i.Value))
                {
                    listItems.Add(new SelectListItem { Text = i.Name, Value = i.Value, Selected = selectedValue.Contains(i.Value) });
                }
            }
            return listItems;
        }

        /// <summary>
        /// 加载文件
        /// </summary>
        /// <param name="path"></param>
        public static ConfigItem Load(string path = null)
        {
            XElement element = XElement.Load(path ?? PathHelper.MapPath(ConfigHelper.GetAppSettingValue("ConfigPath")));
            ConfigItem item = new ConfigItem();
            item.Load(element);
            return item;
        }
    }
}